package com.chengxiaoxiao.web.controller;

import com.chengxiaoxiao.commons.Page;
import com.chengxiaoxiao.constant.Constants;
import com.chengxiaoxiao.domain.Books;
import com.chengxiaoxiao.domain.Category;
import com.chengxiaoxiao.domain.Publisher;
import com.chengxiaoxiao.service.BookService;
import com.chengxiaoxiao.service.CategoryService;
import com.chengxiaoxiao.service.PublisherService;
import com.chengxiaoxiao.service.impl.BookServiceImpl;
import com.chengxiaoxiao.service.impl.CategoryServiceImpl;
import com.chengxiaoxiao.service.impl.PublisherServiceImpl;
import com.chengxiaoxiao.utils.JSONUtils;
import com.chengxiaoxiao.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

//@WebServlet("/manager/BookServlet")
public class BookServlet extends HttpServlet {

    private BookService bookService = new BookServiceImpl();
    private PublisherService publisherService = new PublisherServiceImpl();
    private CategoryService categoryService = new CategoryServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String op = request.getParameter("op");
        if ("listbook".equalsIgnoreCase(op)) {
            listBook(request, response);
        } else if ("showbook".equalsIgnoreCase(op)) {
            showBook(request, response);
        } else if ("deletebook".equalsIgnoreCase(op)) {
            deleteBook(request, response);
        } else if ("showeditBook".equalsIgnoreCase(op)) {
            showeditBook(request, response);
        } else if ("editBook".equalsIgnoreCase(op)) {
            editBook(request, response);
        } else if ("listcategory".equalsIgnoreCase(op)) {
            listCategory(request, response);
        } else if ("showeditCategory".equalsIgnoreCase(op)) {
            showeditCategory(request, response);
        } else if ("editCategory".equalsIgnoreCase(op)) {
            editCategroy(request, response);
        } else if ("deleteCategory".equalsIgnoreCase(op)) {
            deleteCategory(request, response);
        } else if ("addCategory".equalsIgnoreCase(op)) {
            addCategory(request, response);
        } else if ("listPublisher".equalsIgnoreCase(op)) {
            listPublisher(request, response);
        } else if ("addPublisher".equalsIgnoreCase(op)) {
            addPublisher(request, response);
        }
        else if("showeditpublisher".equalsIgnoreCase(op))
        {
            showEditPublisher(request,response);
        }else if("editpublisher".equalsIgnoreCase(op))
        {
            editPublisher(request,response);
        }
        else if("deletePublisher".equalsIgnoreCase(op))
        {
            deletePublisher(request,response);
        }
    }

    /**
     * 删除出版社
     * @param request
     * @param response
     */
    private void deletePublisher(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        int ids = Integer.parseInt(id);

        publisherService.delete(ids);

        HashMap<String, String> map = new HashMap<>();
        map.put(Constants.JSON_RESPONSE_KEY_STATUS, "ok");
        map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "删除成功");
        JSONUtils.toJson(response, map);
    }

    /**
     * 编辑出版社信息
     * @param request
     * @param response
     */
    private void editPublisher(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        String name = request.getParameter("name");

        int ids = Integer.parseInt(id);

        Publisher publisher = new Publisher(ids,name);


        publisherService.update(publisher);

        HashMap<String, String> map = new HashMap<>();
        map.put(Constants.JSON_RESPONSE_KEY_STATUS, "ok");
        map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "修改成功");
        JSONUtils.toJson(response, map);
    }

    /**
     * 查看编辑页面
     * @param request
     * @param response
     */
    private void showEditPublisher(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException  {
        String id = request.getParameter("id");

        Publisher publisher = publisherService.getbyId(Integer.parseInt(id));
        request.setAttribute("publisher", publisher);
        request.getRequestDispatcher("/book/editpublisher.jsp").forward(request, response);
    }

    /**
     * 添加出版社
     *
     * @param request
     * @param response
     */
    private void addPublisher(HttpServletRequest request, HttpServletResponse response) {

        String name = request.getParameter("name");

        Publisher pub = new Publisher();
        pub.setName(name);

        publisherService.add(pub);

        HashMap<String, String> map = new HashMap<>();
        map.put(Constants.JSON_RESPONSE_KEY_STATUS, "ok");
        map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "添加成功");
        JSONUtils.toJson(response, map);
    }

    /**
     * 显示出版社列表
     *
     * @param request
     * @param response
     */
    private void listPublisher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Publisher> list = publisherService.get();

        request.setAttribute("list", list);
        request.getRequestDispatcher("/book/listpublisher.jsp").forward(request, response);
    }

    /**
     * 添加分类信息
     *
     * @param request
     * @param response
     */
    private void addCategory(HttpServletRequest request, HttpServletResponse response) {

        String name = request.getParameter("name");
        String descript = request.getParameter("descript");


        Category category = new Category();
        category.setName(name);
        category.setDescript(descript);

        categoryService.add(category);


        HashMap<String, String> map = new HashMap<>();
        map.put(Constants.JSON_RESPONSE_KEY_STATUS, "ok");
        map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "添加成功");
        JSONUtils.toJson(response, map);
    }

    /**
     * 删除类别
     *
     * @param request
     * @param response
     */
    private void deleteCategory(HttpServletRequest request, HttpServletResponse response) {

        String id = request.getParameter("id");
        int ids = Integer.parseInt(id);

        categoryService.delete(ids);

        HashMap<String, String> map = new HashMap<>();
        map.put(Constants.JSON_RESPONSE_KEY_STATUS, "ok");
        map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "删除成功");
        JSONUtils.toJson(response, map);

    }

    /**
     * 编辑类别
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void editCategroy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String descript = request.getParameter("descript");
        int ids = Integer.parseInt(id);

        Category category = new Category(ids, name, descript);


        categoryService.update(category);

        HashMap<String, String> map = new HashMap<>();
        map.put(Constants.JSON_RESPONSE_KEY_STATUS, "ok");
        map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "修改成功");
        JSONUtils.toJson(response, map);


    }

    /**
     * 显示编辑分类界面
     *
     * @param request
     * @param response
     */
    private void showeditCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");

        Category category = categoryService.getbyId(Integer.parseInt(id));
        request.setAttribute("category", category);
        request.getRequestDispatcher("/book/editcategory.jsp").forward(request, response);
    }

    /**
     * 类别列表
     *
     * @param request
     * @param response
     */
    private void listCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Category> list = categoryService.get();

        request.setAttribute("list", list);
        request.getRequestDispatcher("/book/listcategory.jsp").forward(request, response);

    }

    /**
     * 编辑图书信息
     *
     * @param request
     * @param response
     */
    private void editBook(HttpServletRequest request, HttpServletResponse response) {

        String id = request.getParameter("id");
        String bookName = request.getParameter("bookName");
        String author = request.getParameter("author");
        String publisher = request.getParameter("publisher");
        String number = request.getParameter("number");
        String category = request.getParameter("category");
        String isbn = request.getParameter("isbn");
        String descript = request.getParameter("descript");


        Books book = bookService.getbyId(Integer.parseInt(id));

        book.setBookName(bookName);
        book.setAuthor(author);
        book.setPublisherId(Integer.parseInt(publisher));
        book.setNumber(Integer.parseInt(number));
        book.setCategory_Id(Integer.parseInt(category));
        book.setIsbn(isbn);
        book.setDescript(descript);

        bookService.update(book);

        HashMap<String, String> map = new HashMap<>();
        map.put(Constants.JSON_RESPONSE_KEY_STATUS, "ok");
        map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "修改成功");
        JSONUtils.toJson(response, map);


    }

    /**
     * 查看编辑图书信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void showeditBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        int ids = Integer.parseInt(id);
        Books book = bookService.getbyId(ids);

        List<Publisher> publishers = publisherService.get();
        List<Category> categories = categoryService.get();

        request.setAttribute("category", categories);
        request.setAttribute("publisher", publishers);
        request.setAttribute("book", book);
        request.getRequestDispatcher("/book/editbook.jsp").forward(request, response);
    }

    /**
     * 删除书籍
     *
     * @param request
     * @param response
     */
    private void deleteBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");
        int ids = Integer.parseInt(id);
        bookService.delete(ids);

        HashMap<String, String> map = new HashMap<>();
        map.put(Constants.JSON_RESPONSE_KEY_STATUS, "ok");
        map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "删除成功");
        JSONUtils.toJson(response, map);
    }

    /**
     * 查看书籍
     *
     * @param request
     * @param response
     */
    private void showBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        int ids = Integer.parseInt(id);
        Books book = bookService.getbyId(ids);

        request.setAttribute("book", book);
        request.getRequestDispatcher("/book/showbook.jsp").forward(request, response);
    }

    //显示书籍
    private void listBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String num = request.getParameter("num");
        String bookName = request.getParameter("bookName");

        if (!StringUtils.isNullOrEmpty(bookName)) {
            Page page = bookService.getListPageByBookName(num, bookName);

            page.setUrl(request.getContextPath() + "/manager/BookServlet?op=listbook&bookName=" + bookName);
            request.setAttribute("bookName", bookName);
            request.setAttribute("page", page);

            request.getRequestDispatcher("/book/listbook.jsp").forward(request, response);
        } else {
            Page page = bookService.getListPage(num);

            page.setUrl(request.getContextPath() + "/manager/BookServlet?op=listbook");
            request.setAttribute("page", page);
            request.getRequestDispatcher("/book/listbook.jsp").forward(request, response);
        }
    }
}
